// 引入接口文件
import { getDetail, getgiftList, exportGift } from './giftDetailServ.js'

// 对象和JSON字符串互转
import { parse } from 'qs'
// 弹出消息框
import { message } from 'antd'
// 日期处理对象
import moment from 'moment'
/* isEmpty 对象是否为空
 * isArray 对象是否数组
 * cloneDeep 对象深拷贝
 */
import { isEmpty, isArray, cloneDeep, merge } from 'lodash'
// 引入路由对象
import { hashHistory } from 'dva/router'
import config from 'config/config';
import { download } from 'utils/util'

const queryList = [
	{
		key: 'name',
		name: '客户姓名',
		type: 'text',
	},
	{
		key: 'mobile',
		name: '客户手机号',
		type: 'text',
	},
	{
		name: '是否已领取',
		type: 'select',
		key: 'isReceive',
		value: '',
		notNeedAll: true,
		list: [
			{
				id: '0',
				name: '未领'
			},
			{
				id: '1',
				name: '已领'
			},
			{
				id: '2',
				name: '失效'
			},
		]
	},
	{
		name: '线上申领日期',
		type: 'date',
		key: 'receiveTime',
		value: [],
		double: true,
		options: { showTime: true, format: 'YYYY-MM-DD HH:mm', width: '100' }
	},
	{
		name: '领取方式',
		type: 'select',
		key: 'receiveType',
		value: '',
		notNeedAll: true,
		list: [
			{
				id: '1',
				name: '扫码'
			},
			{
				id: '2',
				name: '二维码'
			},
		]
	},
	
]

// 初始默认状态
const defultState = {
	loading: false,
	btnLoading: false,
	giftId: '',
	detail: {},
	queryList,
	lastQuery: {

	},
	// 表格数据
	tableData: {
		dataSource: [
		],
		total: 0,
		stop: 0,
		start: 0
	},
	// 表格分页设置
	pageConfig: {
		pageSize: Number.parseInt(config.smallPageSizeList[0]),
		pageNo: 1
	},
}
//组合入参函数
function getParmas(queryList, giftId, pageNo = 1, pageSize = 10) {
	let params = { pageNo, pageSize, giftId }
	let splitKey = ["onlineApplyTime", "endOnlineApplyTime"]
	queryList.map((item, idx) => {
		if (item.value && item.value.length > 0) {
			if (item.key != "receiveTime") {
				params[item.key] = item.value
			} else {
				splitKey.forEach((val, i) => {
					params[val] = item.value[i]
				})
			}
		}
	})
	return params
}
// 声明module
const tmpModule = {
	// 默认状态
	state: cloneDeep(defultState),
	// 入口函数(先执行)
	subscriptions: {
		setup({ dispatch, history }) {
			history.listen(({ pathname, query }) => {
				console.log(query)
				// 页面路由匹配时执行
				if ('' + tmpModule.pathname === '' + pathname) {
					dispatch({ type: 'updateStore', payload: cloneDeep(defultState) })
					dispatch({
						type: 'updateStore',
						payload: { giftId: query.giftId }
					}) // end dispatch

					dispatch({
						type: 'updateStore',
						payload: { lastQuery: { ...query } }
					})
					dispatch({
						type: 'giftList',
						payload: { giftId: query.giftId }
					}) // end dispatch
					// 执行业务操作
					dispatch({
						type: 'queryInfo',
						payload: { giftId: query.giftId }
					}) // end dispatch
				} // end if
			}) // end listen
		},
	},

	// 方法列表
	effects: {
		//导出礼品详情列表
		*exportGift({ payload }, { put, call, select }) {
			let { queryList,giftId } = yield select(state => state[tmpModule.namespace])
			//组装接口入参
			let params = {
				pageSize: 100000,
				pageNo: 1,
				giftId
			}

			queryList.map(item => {
				console.log(item)
				// if(item.key=='status'){
				//   item.value=Number(item.value)
				// }
				params[item.key] = item.value
			})
			params.onlineApplyTime=params.receiveTime[0]
      		params.endOnlineApplyTime=params.receiveTime[1]
			yield put({ type: 'updateStore', payload: { exportLoading: true } })
			// let url=`${Config.baseUrl}admin/cooper/export?${stringify(params)}`
			const { data, success } = yield call(exportGift, params)
			if (success) {
				message.success('操作成功！')
				download(data)
			}
			// yield put({ type: 'updateStore', payload: { exportLoading: false } })
		},
		
		// 查询列表
		*giftList({ payload }, { put, call, select }) {
			try {
				let { tableData, pageConfig } = yield select(state => state[tmpModule.namespace])
				const { rows, success, msg, total, pageNo } = yield call(getgiftList, payload)
				if (success) {
					console.log(rows)
					tableData.dataSource = rows
					tableData.total = total
					pageConfig.pageNo = pageNo
					yield put({
						type: 'updateStore',
						payload: {
							tableData,
							pageConfig
						}
					})
				}
			} catch (e) {
				console.error(e)
			}
		},
		// 翻页
		* pageChange({ payload }, { put, select }) {
			const { pageNo, pageSize } = payload
			const { pageConfig, giftId, queryList } = yield select(state => state[tmpModule.namespace])
			yield put({ type: 'updateStore', payload: { pageConfig: { ...pageConfig, ...payload } } })
			yield put({ type: 'giftList', payload: getParmas(queryList, giftId, pageNo, pageSize) })
		},
		//改变查询条件
		* inputChange({ payload }, { call, put, select }) {
			let { queryList } = yield select(state => state[tmpModule.namespace])
			const { key, value } = payload
			let data = queryList.find(item => item.key === key)
			data.value = value
			yield put({ type: 'updateStore', payload: { queryList: [...queryList, ...data] } })
		},
		//搜索
		*search({ payload }, { call, put, select }) {
			let { queryList, giftId } = yield select(state => state[tmpModule.namespace])
			console.log(queryList)
			yield put({ type: 'giftList', payload: getParmas(queryList, giftId) })
		},
		// 查询条件重置
		* queryReset({ }, { put, select }) {
			let { giftId } = yield select(state => state[tmpModule.namespace])
			yield put({ type: 'updateStore', payload: { queryList: cloneDeep(queryList) } })
			yield put({ type: 'giftList', payload: getParmas(queryList, giftId) })
		},

	},

	// 存入状态机
	reducers: {
		updateStore(preState, action) {
			return Object.assign({}, preState, action.payload)
		},
	}
}

export default tmpModule
